//
//  KWLScanCodeWebViewController.m
//  KWNLaywerClient
//
//  Created by 孔六五 on 14/12/14.
//  Copyright (c) 2014年 张明磊. All rights reserved.
//

#import "KWLScanCodeWebViewController.h"

@interface KWLScanCodeWebViewController ()<UIWebViewDelegate>

@property (nonatomic,strong) NSString *scanCodeUrlStr;
@property (weak, nonatomic) UIView *imageView_title;
@property (nonatomic,strong) UIActivityIndicatorView *activityIndicatorView;
@property (nonatomic,strong) UIWebView *webView;

@end

@implementation KWLScanCodeWebViewController
@synthesize scanCodeUrlStr;
@synthesize activityIndicatorView;
@synthesize webView;

- (id)initWithUrlStr:(NSString *)urlstr;
{
    self = [super init];
    if (self) {
        self.scanCodeUrlStr = urlstr;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIView *imageView_title = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44 + IOS7_SIZE)];
    imageView_title.backgroundColor = RGB(91, 165, 229);
    [self.view addSubview:imageView_title];
    _imageView_title = imageView_title;
    
    UIButton *button_menu = [UIButton buttonWithType:UIButtonTypeCustom];
    button_menu.frame = CGRectMake(0, IOS7_SIZE, 60, 45);
    [button_menu setImage:[UIImage imageFileName:@"button_back.png"] forState:UIControlStateNormal];
    //button_menu.tag = KWNDetailViewButtonTag_back;
    [button_menu addTarget:self action:@selector(didClickButton_scancodeWebBack:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button_menu];
    
    webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, _imageView_title.frame.origin.y + _imageView_title.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - _imageView_title.frame.origin.y + _imageView_title.frame.size.height)];
    
    webView.scalesPageToFit =YES;
    webView.delegate =self;
    [self.view addSubview:webView];
    
    activityIndicatorView = [[UIActivityIndicatorView alloc]
                             initWithFrame : CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)] ;
    [activityIndicatorView setCenter: self.view.center] ;
    activityIndicatorView.color = RGB(91, 165, 229);
//    [activityIndicatorView setActivityIndicatorViewStyle: UIActivityIndicatorViewStyleWhite] ;
    [self.view addSubview : activityIndicatorView] ;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self loadWebPageWithString];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)didClickButton_scancodeWebBack:(id)sender
{
    [self popViewController];
}

- (void)loadWebPageWithString
{
    NSURL *url =[NSURL URLWithString:scanCodeUrlStr];
    NSLog(@"%@",scanCodeUrlStr);
    NSURLRequest *request =[NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
}

#pragma mark - UIWebViewDelegate -

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [activityIndicatorView startAnimating] ;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [activityIndicatorView stopAnimating];
}
//页面出错
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
// CS 快法务 KLW 2014-12-30
//    UIAlertView *alterview = [[UIAlertView alloc] initWithTitle:@"出错" message:[error localizedDescription]  delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
//    
    UIAlertView *alterview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"加载出错"  delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];

// CE 快法务 KLW 2014-12-30
    [alterview show];
}

@end
